package com.gridmobi.xrtb.dspcounter;

import com.google.common.collect.Maps;
import org.springframework.stereotype.Component;

import java.util.Map;

@Component
public class DspCounterHandler {
    private static String SPCHAR = "@";
    /*campaignid,creativeid,geo,appid,utchour*/
    public static Map<String, DspCounter> CURRENT_COUNTER = Maps.newConcurrentMap();

    public String key(Integer cid, String crid, String geo, String appid, String os,int ssp) {

        String key = cid + SPCHAR + crid + SPCHAR + geo + SPCHAR + appid + SPCHAR + os + SPCHAR + DspCounterJob.CURRENT_DATE + SPCHAR + DspCounterJob.CURRENT_HOUR+ SPCHAR + ssp;
        return key;
    }

    public  void countBid(Integer cid, String crid, String geo, String appid, String os,int ssp) {

        String key = key(cid, crid, geo, appid, os,ssp);
        if (!CURRENT_COUNTER.containsKey(key)) {
            CURRENT_COUNTER.put(key, new DspCounter());
        }
        CURRENT_COUNTER.get(key).getBids().incrementAndGet();

    }

    public void countWin(Integer cid, String crid, String geo, String appid, String os,int ssp) {

        String key = key(cid, crid, geo, appid, os,ssp);
        if (!CURRENT_COUNTER.containsKey(key)) {
            CURRENT_COUNTER.put(key, new DspCounter());
        }
        CURRENT_COUNTER.get(key).getWins().incrementAndGet();

    }

    public void countImpr(Integer cid, String crid, String geo, String appid, String os,int ssp) {

        String key = key(cid, crid, geo, appid, os,ssp);
        if (!CURRENT_COUNTER.containsKey(key)) {
            CURRENT_COUNTER.put(key, new DspCounter());
        }
        CURRENT_COUNTER.get(key).getImprs().incrementAndGet();

    }

    public void countClicks(Integer cid, String crid, String geo, String appid, String os,int ssp) {

        String key = key(cid, crid, geo, appid, os,ssp);
        if (!CURRENT_COUNTER.containsKey(key)) {
            CURRENT_COUNTER.put(key, new DspCounter());
        }
        CURRENT_COUNTER.get(key).getClicks().incrementAndGet();

    }

    public void countConvs(Integer cid, String crid, String geo, String appid, String os,int ssp) {

        String key = key(cid, crid, geo, appid, os,ssp);
        if (!CURRENT_COUNTER.containsKey(key)) {
            CURRENT_COUNTER.put(key, new DspCounter());
        }
        CURRENT_COUNTER.get(key).getConvs().incrementAndGet();

    }

    public void countCosts(Integer cid, String crid, String geo, String appid, String os, double cost,int ssp) {

        String key = key(cid, crid, geo, appid, os,ssp);
        if (!CURRENT_COUNTER.containsKey(key)) {
            CURRENT_COUNTER.put(key, new DspCounter());
        }
        CURRENT_COUNTER.get(key).getCosts().addAndGet(cost);

    }


}
